package pl.com.sthaether.mc.components.table.filterable;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import pl.com.sthaether.mc.graphic.MCIcon;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public abstract class MCFilterableTableItem<T> implements MCFilterableItem {

    private final SimpleStringProperty name;
    private final SimpleStringProperty description;
    private final SimpleObjectProperty<MCIcon> icon;
    private final SimpleObjectProperty<T> content;
    private final Set<String> keywordsSet;


    public MCFilterableTableItem(String name, T content, String... keywords) {
        this.description = new SimpleStringProperty("");
        this.icon = new SimpleObjectProperty<>(new MCIcon());
        this.name = new SimpleStringProperty(name);
        this.content = new SimpleObjectProperty<>(content);
        this.keywordsSet = new HashSet<>(Arrays.asList(keywords));


    }

    public MCFilterableTableItem(String name, MCIcon icon, String description, T content, String... keywords) {
        this.description = new SimpleStringProperty(description);
        this.icon = new SimpleObjectProperty<>(icon);
        this.name = new SimpleStringProperty(name);
        this.content = new SimpleObjectProperty<>(content);
        this.keywordsSet = new HashSet<>(Arrays.asList(keywords));
    }

    @Override
    public boolean matchesText(String text) {
        String stringToMatch = text.toLowerCase();
        for (String keyword : keywordsSet) {
            if (keyword.toLowerCase().contains(stringToMatch)) {
                return true;
            }
        }
        return name.getValue().toLowerCase().contains(stringToMatch);
    }

    public String getName() {
        return name.getValue();
    }

    public Set<String> getKeywordsSet() {
        return keywordsSet;
    }

    public MCIcon getIcon() {
        return icon.getValue();
    }

    public SimpleObjectProperty<MCIcon> getIconProperty() {
        return icon;
    }

    public SimpleStringProperty getDescriptionProperty() {
        return description;
    }

    public SimpleStringProperty getNameProperty() {
        return name;
    }

    public SimpleObjectProperty<T> getContentProperty() {
        return content;
    }

    public T getContent() {
        return content.getValue();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof MCFilterableTableItem) {
            return getName().equals(((MCFilterableTableItem) obj).getName()) && getKeywordsSet().equals(((MCFilterableTableItem) obj).getKeywordsSet()) && getContent().equals(((MCFilterableTableItem) obj).getContent());
        }
        return false;
    }
}
